//
//  SleepSettingController.m
//  SleepingElf
//
//  Created by karong on 2023/10/23.
//

#import "SleepSettingController.h"
#import "BaseNaolingController.h"
#import "UIButton+ImageAlign.h"

@interface SleepSettingController ()
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *bgArray;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *minLabel;
@property (weak, nonatomic) IBOutlet UIButton *shakeBtn;
@property (weak, nonatomic) IBOutlet UIButton *outPlayBtn;
@property (weak, nonatomic) IBOutlet UIButton *autoCloseBtn;
@property (weak, nonatomic) IBOutlet UIButton *customBtn;
@property (weak, nonatomic) IBOutlet UIButton *playAgainBtn;
@property (weak, nonatomic) IBOutlet UIButton *naolingBtn;

@end

@implementation SleepSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.shakeBtn.selected = appConfig.sleepConfig.shake;
    self.outPlayBtn.selected = appConfig.sleepConfig.outPlay;
    self.autoCloseBtn.selected = appConfig.sleepConfig.autoClose;
    [self layoutTimeBtn:appConfig.sleepConfig.autoCloseTimeType];
    [self addObserver];
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:SESleepSettingNotification object:nil];
    
}

- (void)refreshData {
    [self layoutTimeBtn:appConfig.sleepConfig.autoCloseTimeType];
}

- (void)layoutTimeBtn:(NSInteger)type {
    if (type == 4) {
        NSInteger hour = appConfig.sleepConfig.autoCloseTime / 60;
        NSInteger min = appConfig.sleepConfig.autoCloseTime % 60;
        NSString * hourStr = hour>0?SESF(@"%ldh ", (long)hour):@"";
        NSString * minStr = min>0?SESF(@"%ldmin", (long)min):@"";
        NSString * timeStr = SESF(@"%@%@", hourStr, minStr);
        [self.customBtn setTitle:SEIsNullOrEmpty(timeStr)?WSLocalized(@"自定义"):timeStr forState:(UIControlStateNormal)];
    }
    else {
        [self.customBtn setTitle:WSLocalized(@"自定义") forState:(UIControlStateNormal)];
    }
    
    for (int i = 0; i < 4; i++) {
        UIImageView * image = self.bgArray[i];
        UILabel * label = self.minLabel[i];
        if (type == i) {
            image.image = SEIMG(@"bg_setTime_s");
            label.text = @"min";
        }
        else {
            image.image = SEIMG(@"bg_setTime_n");
            label.text = @"";
        }
    }
    NSString * playAgainStr = (appConfig.sleepConfig.playAgainMint > 0 && appConfig.sleepConfig.playAgainTime > 0)?SESF(WSLocalized(@"%ld分钟%ld次"),(long)appConfig.sleepConfig.playAgainMint, (long)appConfig.sleepConfig.playAgainTime):@"";
    [self.playAgainBtn setTitle:playAgainStr forState:(UIControlStateNormal)];
    [self.naolingBtn setTitle:appConfig.sleepConfig.title forState:(UIControlStateNormal)];
}

- (IBAction)wakeUpMusicAction:(id)sender {
    BaseNaolingController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"BaseNaolingController"];
    target.type = 2;
    [self.navigationController pushViewController:target animated:YES];
}

- (IBAction)vibrationAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    appConfig.sleepConfig.shake = sender.selected;
}

- (IBAction)speakerAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    appConfig.sleepConfig.outPlay = sender.selected;
}

- (IBAction)wakeUpAgainAction:(id)sender {
    
}

- (IBAction)autoCloseAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    appConfig.sleepConfig.autoClose = sender.selected;
}

- (IBAction)setTimeAction:(UIButton *)sender {
    [self layoutTimeBtn:sender.tag];
    appConfig.sleepConfig.autoCloseTimeType = sender.tag;
    NSArray * time = @[@"15",@"30",@"60",@"90"];
    appConfig.sleepConfig.autoCloseTime = [time[sender.tag] integerValue];
}

- (IBAction)customAction:(id)sender {
    UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"PlayerTimePicker"];
    [self presentViewController:target animated:YES completion:nil];
}
@end
